package com.jeff.tianti.controller;

import com.jeff.tianti.cms.dto.LegalCasesQueryDTO;
import com.jeff.tianti.cms.dto.font.LegalCaseFontQueryDTO;
import com.jeff.tianti.cms.entity.ColumnInfo;
import com.jeff.tianti.cms.entity.LegalCases;
import com.jeff.tianti.cms.service.ColumnInfoService;
import com.jeff.tianti.cms.service.LegalCasesService;
import com.jeff.tianti.common.PageBean;
import com.jeff.tianti.common.dto.AjaxResult;
import com.jeff.tianti.common.entity.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/legalCases")
@CrossOrigin(maxAge = 3600)
public class LegalCasesController {
    @Autowired
    LegalCasesService legalCasesService;

    @Autowired
    ColumnInfoService columnInfoService;

    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public AjaxResult legalCasesList(@RequestBody LegalCaseFontQueryDTO legalCaseFontQueryDTO) {
        PageModel<LegalCases> legalCasesPageModel = legalCasesService.queryLegalCasesPage(legalCaseFontQueryDTO);
        List<LegalCases> list = legalCasesPageModel.getList();
        list.forEach(legalCases -> {
            String industryType = legalCases.getIndustryType();
            ColumnInfo industry = columnInfoService.find(industryType);
            legalCases.setIndustryTypeName(industry.getName());
            String mediaType = legalCases.getMediaType();
            ColumnInfo media = columnInfoService.find(mediaType);
            legalCases.setMediaTypeName(media.getName());

        });
        return AjaxResult.setSuccessPageModelDate(legalCasesPageModel);
    }

    @RequestMapping(value = "/detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public AjaxResult legalCasesDetail(@RequestParam(value = "id", required = true) String id) {
        return AjaxResult.setSuccessData(legalCasesService.find(id));
    }

    @RequestMapping(value = "/recommend", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public AjaxResult legalRcommend(@RequestBody LegalCaseFontQueryDTO legalCaseFontQueryDTO) {
        return AjaxResult.setSuccessPageModelDate(legalCasesService.queryLegalCasesPage(legalCaseFontQueryDTO));
    }

}
